package com.appspot.zarm.game;

import java.util.ArrayList;
import java.util.List;

import com.appspot.zarm.gomoku.transfer.GamesList;
import com.appspot.zarm.gomoku.transfer.GamesListExternalizer;

/**
 * This is a storage of all games. You can get any of the game with sessionId of the player.
 * To add new Game you need to have at least one sessionId. 
 * @author Zhardzetski
 *
 */
public class GamesStorage implements GamesListExternalizer {

	private List<Game> games = new ArrayList<Game>();

	public Game getGame(IUser user) {
		for (Game game : games) {
			if (game.hasPlayer(user)) {
				return game;
			}
		}
		return null;
	}


	public void removeGame(IUser user) {
		Game game = getGame(user);
		games.remove(game);
	}

	public void addGame(Game game) {
		games.add(game);
	}

	public boolean isHasPlayer(Player player) {
		if (getGame(player) != null) {
			return true;
		}
		return false;
	}
	
	public GamesList getGamesList() {
		List<String> list = new ArrayList<String>();
		for (Game game: games) {
			list.add(game.getPlayerOne().getId());
		}
		GamesList result = new GamesList();
		result.setList(list);
		return result;
	}
	
	public void save() {
		// empty
	}
	
	public void load(String fileName) {
		// empty
	}

}
